package com.opcode.costatablet.ui.dialogs;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnShowListener;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.opcode.costatablet.R;
import com.opcode.nebulamobil.base.fragments.dialogfragments.DialogFragmentBase;
import com.opcode.nebulamobil.base.interfaces.TimerListener;
import com.opcode.nebulamobil.utility.TimerBase;

/**
 * Bu fragment, sunucuya herhangi bir request gönderildiği zaman çalışır.
 * Bu süreçte ekranda bir yükleme logosu görünür.
 * @author Okan ERDOĞAN
 *
 * 30 Ağu 2014
 */
public class RoundedProgressDialogBase extends DialogFragmentBase implements TimerListener{
	
	private TimerBase timerBase;
	
	public Dialog onCreateDialog(Bundle savedInstanceState){
		
		this.timerBase=new TimerBase(this);
		
		Dialog dialog = super.onCreateDialog(savedInstanceState);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.getWindow().setWindowAnimations(android.R.style.Animation_Dialog);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        dialog.getWindow().getAttributes().dimAmount = 0.55f;
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); 
        
        dialog.setOnShowListener(new OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
				timerBase.schedule(60000);			
			}
		});
        
        dialog.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss(DialogInterface arg0) {
				System.out.println("Timer durduruldu.");
				timerBase.cancelTimer();
			}
		});
        
        dialog.setOnCancelListener(new OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				
			}
		});
        
       
        
		return dialog;
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View dView = inflater.inflate(R.layout.rounded_progress_dialog_layout, container, false);		
		dView.setBackgroundColor(Color.TRANSPARENT);	
		
		return dView;		
	}


	@Override
	public void timeFinished() {
		System.out.println("RoundProgress dialog zaman aşımına uğradı.");
		if(this.getDialog()!=null){
			this.dismiss();
		}	
	}
}
